package com.task.service;

import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.concurrent.*;

/**
 * @author: cxt
 * @time: 2021/7/28
 */
@Service
@Slf4j
public class TaskDemo {
    @Async("myAsyncExecutor")
    public Future<Integer> test1() {
        String name = Thread.currentThread().getName();
        log.info("线程名称：" + name);
        try {
            // 模拟一系列数据操作，耗时1500
            Thread.sleep(1500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return new AsyncResult<>(1500);
    }

    @Async("myAsyncExecutor")
    public Future<Integer> test2() {
        String name = Thread.currentThread().getName();
        log.info("线程名称：" + name);
        try {
            // 模拟一系列数据操作，耗时200
            Thread.sleep(200);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return new AsyncResult<>(200);
    }


    @Async("myAsyncExecutor")
    public Future<Integer> test3() {
        String name = Thread.currentThread().getName();
        log.info("线程名称：" + name);
        try {
            // 模拟一系列数据操作，耗时400
            Thread.sleep(400);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return new AsyncResult<>(400);
    }


    @Async("myAsyncExecutor")
    public Future<Integer> test4() {
        String name = Thread.currentThread().getName();
        log.info("线程名称：" + name);
        try {
            // 模拟一系列数据操作，耗时900
            Thread.sleep(900);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return new AsyncResult<>(900);
    }

    @Async("myAsyncExecutor")
    public Future<Integer> test5() {
        String name = Thread.currentThread().getName();
        log.info("线程名称：" + name);
        try {
            // 模拟一系列数据操作，耗时1000
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return new AsyncResult<>(1000);
    }


    @Test
    public void te() {
        //核心线程数
        int corePoolSize = 3;
        //最大线程数
        int maximumPoolSize = 6;
        //超过 corePoolSize 线程数量的线程最大空闲时间
        long keepAliveTime = 2;
        //以秒为时间单位
        TimeUnit unit = TimeUnit.SECONDS;
        //创建工作队列，用于存放提交的等待执行任务
        BlockingQueue<Runnable> workQueue = new ArrayBlockingQueue<Runnable>(2);

        ThreadPoolExecutor threadPoolExecutor = null;
        try {
            //创建线程池
            threadPoolExecutor = new ThreadPoolExecutor(corePoolSize,
                    maximumPoolSize,
                    keepAliveTime,
                    unit,
                    workQueue,
                    new ThreadPoolExecutor.AbortPolicy());

            //循环提交任务
            for (int i = 0; i < 8; i++) {
                threadPoolExecutor.submit(() -> {
                    String name = Thread.currentThread().getName();
                    System.out.println("大家好，我是线程：" + name);
                    try {
                        //模拟线程执行时间，10s
                        Thread.sleep(10000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                });
                //每个任务提交后休眠500ms再提交下一个任务，用于保证提交顺序
                Thread.sleep(500);
            }

        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            assert threadPoolExecutor != null;
            threadPoolExecutor.shutdown();
        }
    }
}
